<?php
/**
 * 
 * @author tangbl
 *
 */
class CommonAction extends Action {

    /**
     * 初始化
     * 子类重写_initialize方法需要调用parent::_initialize();
     */
    public function _initialize() {
    	
    	$NOW_THEME_PATH = TMPL_PATH.C('DEFAULT_THEME');
    	$NOW_THEME_URL  = TMPL_URL .C('DEFAULT_THEME');
    	
    	$this->assign('NOW_THEME_PATH',$NOW_THEME_PATH);
    	$this->assign('NOW_THEME_URL' ,$NOW_THEME_URL);

		import('ORG.Util.RBAC');
        if (C('USER_AUTH_ON') && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
			RBAC::checkLogin();
            if (!RBAC::AccessDecision()) {
                //检查认证识别号
                if (!$_SESSION [C('USER_AUTH_KEY')]) {
                    //跳转到认证网关
                    redirect(PHP_FILE . C('USER_AUTH_GATEWAY'));
                }
                // 没有权限 抛出错误
                if (C('RBAC_ERROR_PAGE')) {//exit('没有权限');
				   // 定义权限错误页面
				   redirect(C('RBAC_ERROR_PAGE'));
                } else {
                    if (C('GUEST_AUTH_ON')) {
                        $this->assign('jumpUrl', PHP_FILE . C('USER_AUTH_GATEWAY'));
                    }
                    // 提示错误信息
                    $this->error(L('_VALID_ACCESS_'));
                }
            }
        }
    }

   //初始化左侧导航
   public function _initmenu(){
   		//得到左侧导航菜单的数组
	    $value    = RBAC::getAccessMenu();
	    $menuid   = C("KEEP_MENU");
	    $nowId    = $this->_param("$menuid","intval",0);
	    if($nowId){
	    	session("$menuid",$nowId);
	    }else{
	    	$nowId = session("$menuid");
	    }
		$leftmenu = RBAC::getLeftMenu($value,$menuid,$nowId);
		
		$this->assign("menu_tree",$leftmenu);
	}
	
	//定义空操作
	public function _empty(){
		$this->error(L('cfg_form_action_noexsits'));
	}
	
	
	/**
	 * 公共删除方法
	 * @author tangbl
	 * @param  $table 表名
	 * @param  $param 参数
	 * @param  $flag(true物理删除 false逻辑删除)
	 * $param = array(
           "reply"  => false,//缓存回复   
           "after"  => "Article_del_after"//后置方法
           "cache"  => false,// 缓存数据
    	);
	 * 
	**/   
	protected function do_del($table,$flag=true,$param=array()){
		$id = I('param.id',0,'');//获取id值
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$reply = isset($param["reply"]) ? $param["reply"] : false;
		$cache = isset($param["cache"]) ? $param["cache"] : false;
		$after = @$param["after"];
		
		$model   = M("$table");
		$where   = "id in ($id)";
		
		if($flag){
			$res  = $model->where($where)->delete();
		}else{
			$map["isdel"] = 1 ;
			$res  = $model->where($where)->save($map);
		}

		if($res) {
			
			//删除数据缓存
			if($cache){
				deldir(DATA_PATH."/".$table);
			}
			
			//删除缓存回复
			if($reply){
				$lists = $model->where($where)->field("title,solevar")->select();
				foreach($lists as $v){
					create_reply_content($v,false);
				}
			}
			
			load("@.aop");
			//后置操作
			if(function_exists($after)){
				call_user_func($after,$table,$id);
			}
			
			//记录日志
			wlog(1,"",$id);
			$this->success(L('cfg_form_tipmsg_delsucc'));
		} else {
			
			wlog(2,"",$id);
			$this->error(L('cfg_form_tipmsg_delfail'));
		}

    }
    
    /**
     * 公共修改方法
     * @author tangbl
     * @param  $table 表名
     * @param  $param 参数
	  * $param = array(
	    	"upload" => array(
	    			"single"=>array("single"),
	    			"atlas" =>array("atlas"),
	    			"attach"=>array("attach")
	    			), //上传	
	         "reply"  => false, // 缓存回复  
	         "cache"  => false, // 缓存数据
	         "url"    => array("url"=>"Index/index","param"=>"cateid=1"),//自定义链接
	         "arr"    => array("title"=>"demo","cateid=1"),//额外字段 
	         "after"  => "Article_add_after" //后置方法
	         "before" => "Article_add_before"//前置方法
    	);
	**/ 
    protected function do_edit($table,$param=array()){
    	
    	$reply = isset($param["reply"]) ? $param["reply"] : false;
    	$cache = isset($param["cache"]) ? $param["cache"] : false;
    	$image = @$param["upload"];
    	$url   = @$param["url"];
    	$arr   = @$param["arr"];
    	$before= @$param["before"];
    	$after = @$param["after"];
    	
    	$t = D("$table");
    	if($t->create()) {
    		$solevar = $t->solevar;
    		$title   = $t->title;
    		$id      = $this->_param("id","intval",0);
    		if(is_array($image)){
    			$single = @$image['single'];
    
    			if(is_array($single)){
    				foreach($single as $v){
    					add_edit_thumb($v, $solevar, $table);//添加缩略图
    				}
    			}$atlas = @$image['atlas'];
    			if(is_array($atlas)){
    				foreach($atlas as $v){
    					add_edit_album($v, $solevar, $table);//添加图集
    				}
    			}
    			$attach = @$image['attach'];
    			if(is_array($attach)){
    				foreach($attach as $v){
    					add_edit_file($v, $solevar, $table);//添加附件
    				}
    			}
    		}
    			
    		if(is_array($arr)){
    			foreach($arr as $k=>$v){
    				$t->$k=$v;
    			}
    		}
    		
    		//前置操作
    		load("@.aop");
    		if($before && function_exists($before)){
    			$t = call_user_func($before,$t);
    		}
    		
    		$result = $t->save();
    		
    		//删除数据缓存
    		if($cache){
    			deldir(DATA_PATH."/".$table);
    		}
    		
    		//更新缓存回复
    		if($reply){
    			$arrreply = array("solevar"=>$solevar,"title"=>$title);
    			create_reply_content($arrreply,true);
    		}
    		
    		//后置操作
    		if($after && function_exists($after)){
    			call_user_func($after,$table,$id);//,&$url
    		}
    			
    		//记录日志
    		wlog(1,"",$id);
    			 
    		if(is_array($url)){
    			$url =U($url['url'],$url['param']);
    		}else{
    			$url =U($table.'/index');
    		}
    		$this->assign("jumpUrl",$url);
    		$this->success(L('cfg_form_tipmsg_editsucc'));
	
    	}else{
    		wlog(2,"",$t->getError());
    		$this->error($t->getError());
    	}
    }

	
    /**
	 * 公共添加方法
	 * @author tangbl
	 * @param  $table 表名
	 * @param  $param 参数
	 * $param = array(
    	"upload" => array(
    			"single"=>array("single"),
    			"atlas" =>array("atlas"),
    			"attach"=>array("attach")
    			), //上传	
         "reply"  => false,// 缓存回复  
         "cache"  => false,// 缓存数据
         "url"    => array("url"=>"Index/index","param"=>"cateid=1"),//自定义链接
         "arr"    => array("title"=>"demo","cateid=1"),//额外字段 
         "after"  => "Article_add_after" //后置方法
         "before" => "Article_add_before"//前置方法
    	);
	**/
    protected function do_add($table,$param=array()){
    	
    	$reply = isset($param["reply"]) ? $param["reply"] : false;
    	$cache = isset($param["cache"]) ? $param["cache"] : false;
    	$image = @$param["upload"];
    	$url   = @$param["url"];
    	$arr   = @$param["arr"];
    	$before= @$param["before"];
    	$after = @$param["after"];
    	
        $t = D("$table");
        if($t->create()){
			$solevar = $t->solevar;
			$title   = $t->title;
			
			$t->lang = $this->_param("lang","",getLang());
			if(is_array($image)){

				$single = @$image['single'];
				if(is_array($single)){
					foreach($single as $v){
						add_edit_thumb($v, $solevar, $table);//添加缩略图
					}	
				}
				
				$atlas = @$image['atlas'];
				if(is_array($atlas)){
					foreach($atlas as $v){
						add_edit_album($v, $solevar, $table);//添加图集
					}	
				}
				
				$attach = @$image['attach'];
				if(is_array($attach)){
					foreach($attach as $v){
						add_edit_file($v, $solevar, $table);//添加文件
					}
				}
			}
			
            if(is_array($arr)){
               foreach($arr as $k=>$v){
                $t->$k=$v;
               }
            }
            //前置操作
            load("@.aop");
            if($before && function_exists($before)){
            	$t = call_user_func($before,$t);
            }//dump($t);exit();
            
            $t->addid = session(C('USER_AUTH_KEY'));
            $result   = $t->add();
			
            if($result){
            	
            	//删除数据缓存
            	if($cache){
            		deldir(DATA_PATH."/".$table);
            	}
            	
				//更新缓存回复
				if($reply){
					$arrreply = array("solevar"=>$solevar,"title"=>$title);
					create_reply_content($arrreply,true);
				}
				
				//后置操作		
				if($after && function_exists($after)){
					call_user_func($after,$table,$result);//,&$url
				}
				
				//记录日志
				wlog(1,"",$result);
				
				if(is_array($url)){
					if(@$url['append']){
						$url['param'] .= $result ;
					}
					$url =U($url['url'],$url['param']);
				}else{
					$url =U($table.'/index');
				}
				
				$this->assign("jumpUrl",$url);   
				$this->success(L('cfg_form_tipmsg_addsucc'));
            }else{
            	
            	wlog(2,"",$result);
                $this->error(L('cfg_form_tipmsg_addfail'));
            }
        }else{
        	
        	wlog(2,"",$t->getError());
            $this->error($t->getError());
        }
    }
    
     /**
     *  获取输出页面内容
     * 调用内置的模板引擎fetch方法，
     * @access protected
     * @param string $templateFile 指定要调用的模板文件
     * 默认为空 由系统自动定位模板文件
     * @param string $content 模板输出内容
     * @param string $prefix 模板缓存前缀* 
     * @return string
     */
	protected function fetch1($templateFile='',$content='',$prefix='') {
		
		$depr          = C('TMPL_FILE_DEPR');
		$TMPL_LANG_ARR = C('TMPL_LANG_ARR');
		
		if(empty($templateFile)) {
			// 如果模板文件名为空 按照默认规则定位
			$template = MODULE_NAME . $depr . ACTION_NAME;
		}elseif(strpos('|'.$templateFile,":")){
			$template = str_replace(':', $depr, $templateFile);
		}else{
			$template = MODULE_NAME . $depr .$templateFile;
		}
		
		$lang_file = $TMPL_LANG_ARR[LANG_SET];
		if($lang_file){
			$LANG_PATH = $lang_file.'/';
			$url_suffix = C('TMPL_TEMPLATE_SUFFIX');
			$tpl_file   = TMPL_PATH.$LANG_PATH.$template.$url_suffix;
		
			if(!file_exists($tpl_file)){
				$lang       = C('DEFAULT_LANG');
				$lang_file  = $TMPL_LANG_ARR[$lang];
			}
			$LANG_PATH = $lang_file.'/';
		}else{
			$LANG_PATH = '/';
		}
		$templateFile = $LANG_PATH.$template;
		return $this->view->fetch($templateFile,$content,$prefix);
	}
}